package com.fujifilm.project.plan.config;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.fujifilm.project.plan.error.BusinessException;
import com.fujifilm.project.plan.utils.HttpResponse;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 业务异常处理
     * 
     * @param e
     * @param request
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public HttpResponse<Void> handleBusinessException(BusinessException e, HttpServletRequest request) {
        log.warn("Business exception occurred: {} at {}", e.getMessage(), request.getRequestURI());
        return HttpResponse.error(e.getCode(), e.getMessage());
    }

    /**
     * 非预期异常处理
     * 
     * @param e
     * @param request
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public HttpResponse<Void> handleGenericException(Exception e, HttpServletRequest request) {
        log.error("Unexpected exception occurred at {}", request.getRequestURI(), e);
        return HttpResponse.error(500, "系统异常，请联系管理员");
    }
}
